package com.zhn;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 给你一棵二叉树的根节点 root ，翻转这棵二叉树，并返回其根节点
 */

public class InvertTree {
    public TreeNode invertTree(TreeNode root) {
        if(root == null){
            return null;
        }
        Queue<TreeNode> que = new LinkedList<>();
        que.add(root);
        while(!que.isEmpty()){
            int len = que.size();
            while(len-- > 0){
                TreeNode poll = que.poll();
                reverse(poll);
                if(poll.left != null) que.add(poll.left);
                if (poll.right != null)que.add(poll.right);
            }
        }
        return root;
    }
    public void reverse(TreeNode node){
        TreeNode tmp = node.left;
        node.left = node.right;
        node.right = tmp;
    }
}
